﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CatClient.Message;
using CatClient.Util;
using CatClient;
using PostSharp.Aspects;
using PostSharp.Aspects.Internals;
using PostSharp.Extensibility;

namespace PsCat.Aspet
{
	// Token: 0x0200000B RID: 11
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
	[HasInheritedAttribute(new long[]
	{

	})]
	[Serializable]
	public class UrlCatAttribute : CatBaseAttribute
	{
		// Token: 0x0600002F RID: 47 RVA: 0x00002A47 File Offset: 0x00000C47
		public UrlCatAttribute(string name = "", string desc = "")
		{
			this._name = name;
			this._desc = desc;
		}

		// Token: 0x06000030 RID: 48 RVA: 0x00002A60 File Offset: 0x00000C60
		[HasInheritedAttribute(new long[]
		{
			-966559710620335241L
		})]
		[MethodExecutionAdviceOptimization(MethodExecutionAdviceOptimizations.None)]
		public override void OnEntry(MethodExecutionArgs args)
		{
			try
			{
				if (Cat.IsInitialized())
				{
					List<ITransaction> list = Cat.NewEntryTransaction(new PostsharpEntryTranProcesser(this, args, this._name, this._desc), true);
					ITransaction controllerTransaction = this.GetControllerTransaction(args);
					if (controllerTransaction != null)
					{
						list.Add(controllerTransaction);
					}
					args.MethodExecutionTag = list;
				}
			}
			catch (Exception ex)
			{
				LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Error(ex.Message, ex);
			}
		}

		// Token: 0x06000031 RID: 49 RVA: 0x00002AD8 File Offset: 0x00000CD8
		[HasInheritedAttribute(new long[]
		{
			-966559711310300280L
		})]
		[MethodExecutionAdviceOptimization(MethodExecutionAdviceOptimizations.IgnoreGetMethod | MethodExecutionAdviceOptimizations.IgnoreSetFlowBehavior | MethodExecutionAdviceOptimizations.IgnoreGetArguments | MethodExecutionAdviceOptimizations.IgnoreSetArguments | MethodExecutionAdviceOptimizations.IgnoreGetInstance | MethodExecutionAdviceOptimizations.IgnoreSetInstance | MethodExecutionAdviceOptimizations.IgnoreGetException | MethodExecutionAdviceOptimizations.IgnoreGetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetMethodExecutionTag | MethodExecutionAdviceOptimizations.IgnoreGetYieldValue | MethodExecutionAdviceOptimizations.IgnoreSetYieldValue | MethodExecutionAdviceOptimizations.IgnoreGetDeclarationIdentifier)]
		public override void OnExit(MethodExecutionArgs args)
		{
			List<ITransaction> list = (List<ITransaction>)args.MethodExecutionTag;
			if (list == null || list.Count == 0)
			{
				return;
			}
			try
			{
				list.Reverse();
				foreach (ITransaction transaction in from tt in list
				where !tt.IsCompleted()
				select tt)
				{
					transaction.Complete();
				}
			}
			catch (Exception ex)
			{
				LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Error(ex.Message, ex);
			}
		}

		// Token: 0x06000032 RID: 50 RVA: 0x00002B8C File Offset: 0x00000D8C
		[HasInheritedAttribute(new long[]
		{
			-966559711086782217L
		})]
		[MethodExecutionAdviceOptimization(MethodExecutionAdviceOptimizations.IgnoreGetMethod | MethodExecutionAdviceOptimizations.IgnoreSetFlowBehavior | MethodExecutionAdviceOptimizations.IgnoreGetArguments | MethodExecutionAdviceOptimizations.IgnoreSetArguments | MethodExecutionAdviceOptimizations.IgnoreGetInstance | MethodExecutionAdviceOptimizations.IgnoreSetInstance | MethodExecutionAdviceOptimizations.IgnoreGetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetMethodExecutionTag | MethodExecutionAdviceOptimizations.IgnoreGetYieldValue | MethodExecutionAdviceOptimizations.IgnoreSetYieldValue | MethodExecutionAdviceOptimizations.IgnoreGetDeclarationIdentifier)]
		public override void OnException(MethodExecutionArgs args)
		{
			List<ITransaction> list = (List<ITransaction>)args.MethodExecutionTag;
			if (list == null || list.Count == 0)
			{
				return;
			}
			try
			{
				foreach (ITransaction transaction in list)
				{
					if (args.Exception is ThreadAbortException)
					{
						transaction.Status = "0";
					}
					else
					{
						transaction.SetStatus(args.Exception);
					}
				}
			}
			catch (Exception ex)
			{
				LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Error(ex.Message, ex);
			}
		}

		// Token: 0x06000033 RID: 51 RVA: 0x00002C3C File Offset: 0x00000E3C
		private ITransaction GetControllerTransaction(MethodExecutionArgs args)
		{
			try
			{
				if (args.Method.DeclaringType != null && args.Method.DeclaringType.Name.EndsWith("Controller"))
				{
					string name = string.Format("{0}.{1}", args.Method.DeclaringType.Name, args.Method.Name);
					ITransaction result = Cat.NewTransaction("Controller", name);
					Cat.LogEvent("Controller", name, Cat.Success, this.MethodParams(args));
					return result;
				}
			}
			catch (Exception exception)
			{
				LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Error("[GetControllerTransaction] 创建Controller Transaction失败", exception);
			}
			return null;
		}

		// Token: 0x04000011 RID: 17
		private readonly string _desc;

		// Token: 0x04000012 RID: 18
		private readonly string _name;
	}
}
